<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="文章标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入文章标题" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" placeholder="请输入作者" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="12">
          <el-form-item label="分类" prop="categoryId">
            <el-select v-model="form.categoryId" placeholder="请选择分类" style="width: 100%;">
              <el-option
                  v-for="item in categoryList"
                  :key="item.categoryId"
                  :label="item.categoryName"
                  :value="item.categoryId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标签" prop="tags">
            <el-select v-model="form.tags" multiple placeholder="请选择标签" style="width: 100%;">
              <el-option
                  v-for="item in tagList"
                  :key="item.tagId"
                  :label="item.tagName"
                  :value="item.tagId" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="12">
          <el-form-item label="副标题" prop="subtitle">
            <el-input v-model="form.subtitle" placeholder="请输入副标题" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="来源" prop="source">
            <el-input v-model="form.source" placeholder="请输入来源" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="摘要" prop="summary">
        <el-input v-model="form.summary" type="textarea" placeholder="请输入摘要" />
      </el-form-item>
      
      <el-form-item label="关键词" prop="keywords">
        <el-input v-model="form.keywords" placeholder="请输入关键词，多个关键词用逗号分隔" />
      </el-form-item>

      <el-form-item label="封面图片" prop="coverImage">
        <image-upload
            v-model="form.coverImage"
            :limit="1"
            :file-size="5"
            :disabled="false"
        />
      </el-form-item>
      
      <el-form-item label="文章内容" prop="content">
        <editor v-model="form.content" :min-height="192" />
      </el-form-item>
      
      <el-row>
        <el-col :span="8">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio label="0">草稿</el-radio>
              <el-radio label="1">发布</el-radio>
              <el-radio label="2">撤回</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否置顶" prop="isTop">
            <el-radio-group v-model="form.isTop">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否热门" prop="isHot">
            <el-radio-group v-model="form.isHot">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker
          v-model="form.publishTime"
          type="datetime"
          placeholder="选择发布时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 100%;"
        />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getArticle, updateArticle } from "@/api/cms/article";
import { listCategory } from "@/api/cms/category"; // 添加这行
import { listTag } from "@/api/cms/tag"; // 添加这行
import Editor from '@/components/Editor';
import ImageUpload from '@/components/ImageUpload/index.vue'; // 引入组件
import { ElMessage } from 'element-plus';

export default {
  name: "ArticleEdit",
  components: {
    Editor,
    ImageUpload // 注册组件
  },
  data() {
    return {
      // 表单参数
       baseURL: import.meta.env.VUE_APP_BASE_API,
      categoryList: [],
      tagList: [], //  标签列表
      form: {
        articleId: null,
        categoryId: null,
        title: null,
        subtitle: null,
        summary: null,
        content: null,
        author: null,
        coverImage: null,
        keywords: null,
        source: null,
        status: "0",
        isTop: "0",
        isHot: "0",
        viewCount: 0,
        likeCount: 0,
        commentCount: 0,
        publishTime: null,
        remark: null,
        tags: []
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "文章标题不能为空", trigger: "blur" }
        ],
        author: [
          { required: true, message: "作者不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "分类不能为空", trigger: "change" }
        ],
        content: [
          { required: true, message: "文章内容不能为空", trigger: "blur" }
        ]
      }

    };
  },
  created() {
    const articleId = this.$route.params.articleId;
    this.getArticleInfo(articleId);
    this.getCategoryList();
    this.getTagList();
  },
  methods: {
    // 获取文章详情
    getArticleInfo(articleId) {
      getArticle(articleId).then(response => {
        this.form = response.data;
      });
    },

    // 获取分类列表
    getCategoryList() {
      listCategory().then(response => {
        this.categoryList = response.rows || response.data;
      });
    },

    getTagList() {
      // 假设有获取标签的API，如果没有需要创建相应的API
      listTag().then(response => {
        this.tagList = response.rows || response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.$router.push('/cms/article');
    },
    // 表单重置
    reset() {
      this.form = {
        articleId: null,
        categoryId: null,
        title: null,
        subtitle: null,
        summary: null,
        content: null,
        author: null,
        coverImage: null,
        keywords: null,
        source: null,
        status: "0",
        isTop: "0",
        isHot: "0",
        viewCount: 0,
        likeCount: 0,
        commentCount: 0,
        publishTime: null,
        remark: null,
        tags: []
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateArticle(this.form).then(response => {
            ElMessage.success("修改成功");
            this.$router.push('/cms/article');
          });
        }
      });
    }
  }
};
</script>

<style scoped>

</style>